<?php

namespace maotool\core;

use maotool\cache\Driver;
use maotool\cache\TagSet;

class Cache
{

    /**
     * 获取缓存
     * @param $key string
     * @param $default mixed
     * @return mixed
     */
    public function get($key, $default = null)
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        return $driver->get($key, $default);
    }

    /**
     * 缓存标签
     * @param array $tags
     * @return TagSet
     */
    public function tags($tags)
    {
        return new TagSet($tags);
    }

    /**
     * 缓存是否存在
     * @param string $key
     * @return bool
     */
    public function has($key)
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        return $driver->has($key);
    }

    /**
     * 获取并删除缓存
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public function pull($key, $default = null)
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        $value = $driver->get($key, $default);
        $driver->remove($key);
        return $value;
    }

    /**
     * 设置缓存 如果缓存不存在则创建
     * @param string $key
     * @param mixed $value
     * @param int $seconds
     * @return bool
     */
    public function put($key, $value, $seconds = null)
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        return $driver->save($key, $value, $seconds);
    }

    /**
     * 保存缓存 如果缓存不存在则创建
     * @param string $key
     * @param mixed $value
     * @param int $seconds
     * @return bool
     */
    public function save($key, $value, $seconds = null)
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        return $driver->save($key, $value, $seconds);
    }

    /**
     * 替换缓存 如果缓存不存在则报错
     * @param string $key
     * @param mixed $value
     * @param int $seconds
     * @return bool
     */
    public function replace($key, $value, $seconds = null)
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        return $driver->update($key, $value, $seconds);
    }

    /**
     * 设置不存在的缓存
     * @param string $key
     * @param mixed $value
     * @param int $seconds
     * @return bool
     */
    public function add($key, $value, $seconds = null)
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        return $driver->create($key, $value, $seconds);
    }

    /**
     * 递增缓存
     * @param string $key
     * @param int $value
     * @return int|bool
     */
    public function increment($key, $value = 1)
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        return $driver->increment($key, $value);
    }

    /**
     * 递减缓存
     * @param string $key
     * @param int $value
     * @return int|bool
     */
    public function decrement($key, $value = 1)
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        return $driver->decrement($key, $value);
    }

    /**
     * 删除缓存
     * @param string $key
     * @return bool
     */
    public function remove($key)
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        return $driver->remove($key);
    }

    /**
     * 清空缓存
     * @return bool
     */
    public function clear()
    {
        /** @var Driver $driver */
        $driver = Container::getInterface(Driver::class);
        return $driver->clear();
    }

}